<template>
    <div class="goodsDetail">
        <!-- <h3>商品详情-{{ id }}</h3> -->
        <div class="card" @click="previewImage">
            <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
                <van-swipe-item v-for="(item, index) in thumb" :key="index">
                    <img :src="item.src" />
                </van-swipe-item>
            </van-swipe>
        </div>
        <div class="card">
            <div class="price">
                <span class="sell_price">&yen; {{ goodsInfo.sell_price }}</span>
                <span class="market_price">&yen; {{ goodsInfo.market_price }}</span>
            </div>
            <div class="title van-multi-ellipsis--l2">
                {{ goodsInfo.title }}
            </div>
        </div>
        <div class="card">
            <van-divider content-position="left">介绍</van-divider>
            <div class="content" v-html="goodsInfo.content"></div>
        </div>
        <van-sku v-model="show" :sku="sku" :goods="goods" :goods-id="goodsInfo.id" :hide-stock="sku.hide_stock"
            :reset-stepper-on-hide="true" :show-add-cart-btn="showAddCartBtn" @add-cart="skuAddCart"
            @buy-clicked="skuBuy">
            <template #sku-messages>
                <div class="card">
                    <van-divider>文字</van-divider>
                    <div>商品货号：{{ goodsInfo.goods_no }}</div>
                    <div>库存：{{ goodsInfo.stock_quantity }}件</div>
                    <div>上架时间：{{ goodsInfo.add_time | dateFormat('YYYY-MM-DD') }}</div>
                </div>
            </template>
        </van-sku>
    </div>
</template>

<script>
import { fetchGoodsImages, fetchGoodsInfo } from "../api/index.js"
import { ImagePreview } from 'vant';
export default {
    props: ['id'],
    data() {
        return {
            show: false, // 显示sku的弹窗
            thumb: [],
            showAddCartBtn: true, // 是否显示sku中加入购物车的按钮
            goodsInfo: {},
            sku: {
                // 数据结构见下方文档
                tree: [],
                price: "879.00", // 默认价格（单位元）
                stock_num: 18, // 商品总库存
                hide_stock: false, // 是否隐藏剩余库存
            },
            goods: {
                // 数据结构见下方文档
                picture: ""
            }
        }
    },
    methods: {
        callPhone() {
            // 询问
            this.$dialog.confirm({
                title: '拨打电话',
                message: '确认要联系客服吗',
            })
                .then(() => {
                    // on confirm
                    window.location.href = "tel://" + 13413132233;
                })
                .catch(() => {
                    // on cancel
                });

        },
        async _fetchGoodsImages() {
            let { message } = await fetchGoodsImages(this.id)
            this.thumb = message
            // 赋值给sku商品图片
            this.goods.picture = message.length && message[0].src
        },
        async _fetchGoodsInfo() {
            let { message } = await fetchGoodsInfo(this.id)
            let reg = /\width=['"]\w+['"]\s+/i;
            message.content = message.content.replace(reg, '')
            this.goodsInfo = message;
            this.sku.price = message.sell_price;
        },
        previewImage() {
            // 图片预览
            let imgs = this.thumb.map(item => item.src);
            ImagePreview({
                images: imgs,
                startPosition: 1,
            });
        },
        toCar(showAddCartBtn) {
            // 显示sku,并显示sku中加入购物车按钮
            this.show = true;
            this.showAddCartBtn = showAddCartBtn;
        },
        toBuy(showAddCartBtn) {
            // 显示sku
            this.show = true;
            this.showAddCartBtn = showAddCartBtn;
        },
        skuAddCart(skudata) {
            // console.log(skudata)
            // 准备购物车商品数据
            let { goodsId, selectedNum } = skudata
            let { sell_price } = this.goodsInfo;
            let cartItem = { id: goodsId, number: selectedNum, sell_price, selected: true }
            // 添加到vuex进行存储即可
            console.log(cartItem)
            this.$store.commit('addGoodsToCar', cartItem)
            this.show = false; // 隐藏sku弹窗

        },
        skuBuy(skudata) {
            let { goodsId, selectedNum } = skudata
            let { sell_price } = this.goodsInfo;
            let cartItem = { id: goodsId, number: selectedNum, sell_price, selected: true }
            this.$store.commit('addGoodsToCar', cartItem)
            this.$router.push('/home/shopcar')
        },
    },
    created() {

        this._fetchGoodsImages()
        this._fetchGoodsInfo()
    }
}
</script>

<style lang="scss" scoped>
@import "../assets/scss/var.scss";

.goodsDetail-container {
    height: 100vh;
    background: $bgColor;
    padding: 4px;

    .card {
        border-radius: 6px;
        margin-bottom: 8px;
        background: #fff;
        padding: 8px;

        .my-swipe {
            .van-swipe-item {
                height: 240px;
                text-align: center;
                img {
                    // width: 100%;
                    height: 100%;
                }
            }
        }

        .price {
            .sell_price {
                color: #fa865d;
                font-size: 20px;
                margin-right: 20px;
            }

            .market_price {
                color: #666;
                font-size: 14px;
            }
        }

        .title {
            color: #333;
            font-weight: 500;
        }

        .content {
            color: #666;

            ::v-deep img {
                width: 100%;
                height: 100%;
            }
        }
    }

}
</style>